<?php
class pager{

	var $_currentPage = 1;//当前页
	var $_totalPages = 1;	//总页数
	var $_perPage ;		//每页显示数目
	var $_startRecord = 0;	
	var $_endRecord = 0 ;
	var $_totalRecord = 0;//总纪录数目

	function pager($TotalRecord,$PerPage,$currentPage){
		if(!$currentPage)$currentPage=1;
		$this->_totalRecord=$TotalRecord;
		$this->_perPage=$PerPage;
		$this->_currentPage=$currentPage;
		$this->_totalPages=ceil($TotalRecord/$PerPage)>0?ceil($TotalRecord/$PerPage):1;
		//$this->_startRecord=($currentPage-1)*$PerPage;
		// 当当前页大于总页数的时候会有问题，故将上面注释行改为如下两行
		// 
		$this->_currentPage = $currentPage > $this->_totalPages?$this->_totalPages:$currentPage;
		$this->_startRecord=($this->_currentPage-1)*$PerPage;

		$this->_endRecord = $this->_startRecord + $PerPage;
	}
	function create_jump_menu(){
		/*
		20050321_084800 add first page and last page button
		        <input type=button value='首页' class='' onclick=\"location.href='$URL"."1'\" style='height:20'> 
		<input type=button value='下页 >>' class='' onclick=\"location.href='$URL".($this->_currentPage+1)."'\" ";
		if($this->_currentPage+1>$this->_totalPages)print " disabled";
		print " style='height:20'>
				<input type=button value='<< 上页' class='' onclick=\"location.href='$URL".($this->_currentPage-1)."'\" ";
		if($this->_currentPage-1<1)print " disabled";
		print " style='height:20'>
		<input type=button value='末页' class='' onclick=\"location.href='$URL".$this->_totalPages."'\" style='height:20'>
		";class=lbr_border
		*/
		$query=$this->get_query_string("page");
		if($query)$URL=$_SERVER[PHP_SELF]."?".$query."&page=";
		else $URL=$_SERVER[PHP_SELF]."?page=";

		print "<table width='100%' bgcolor='#E7F0F1' cellpadding='0' cellspacing='1' align='center' class='text-one'>
		<tr><td	align=center>";
        printf("<a href='%s'>首页</a> | ",$URL."1");
		if($this->_currentPage+1>$this->_totalPages)print "下页 | ";
		else
		printf("<a href='%s'>下页</a> | ",$URL.($this->_currentPage+1));

		if($this->_currentPage-1<1)print "上页 | ";
		else
        printf("<a href='%s'>上页</a> | ",$URL.($this->_currentPage-1));

		printf("<a href='%s'>末页 </a>",$URL.$this->_totalPages);

		$to=$this->_currentPage*$this->_perPage;
		if($to>$this->_totalRecord)$to=$this->_totalRecord;
		print "当前显示：".(($this->_currentPage-1)*$this->_perPage+1)."-".$to."条";

		print "　跳到：<input type=text size=4  onChange=\"my_pager_jump_abc(this.value)\" value=".$this->_currentPage." onfocus='this.select()' style='height:20' onkeypress='if(event.keyCode==13)my_pager_jump_abc(this.value);'>
		<select onChange=\"my_pager_jump_xy(this.value)\" style='height:20;font-size:9pt'>\n";
		$start=$this->_currentPage-20<1?1:$this->_currentPage-20;
		$end=$this->_currentPage+20>$this->_totalPages?$this->_totalPages:$this->_currentPage+20;
		for($i=$start;$i<=$end;$i++){
			if($i==$this->_currentPage)print "<option value=$i selected>$i 页</option>\n";
			else print "<option value=$i>$i 页</option>\n";
		}
		print "</select>\n
		当前页/总页数：$this->_currentPage/$this->_totalPages 总数：$this->_totalRecord
		</td></tr></table>\n";
		print "	<script language=\"JavaScript\">\n";
		print "function my_pager_jump_xy(page){ \n";
		print " location.href='$URL'+page;\n";
		print " }\n
		function my_pager_jump_abc(page){
			if(page>$this->_totalPages){
				alert('该页不存在');
			}
			else location.href='$URL'+page;\n
		}
		</script>\n";
	}

	function show_link(){//取一个贴切一点的名字^_^
		$this->create_jump_menu();
	}


	function get_query_string($var='',$var2=''){
		reset($_POST);
		while(list($key,$value)=each($_POST)){
			if($key&&$value){
				$str.=$key."=".urlencode($value)."&";	 
			}		
		}	
		$str.=$_SERVER[QUERY_STRING];

		$query=explode("&",$str);
		for($i=0;$i<count($query);$i++){
			$a=explode("=",$query[$i]);
			if($a[0]!=$var&&$a[0]!=$var2){
				$string.=$query[$i]."&";
			}
		}
		return substr($string,0,-1);

	}
}
?>